package server;

import com.example.demo.pojo.Account;
import com.example.demo.pojo.Friend;
import com.example.demo.pojo.Messages;
import com.example.demo.pojo.Msg;
import com.alibaba.fastjson.JSONObject;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class clientHandler extends Thread{
    private Socket clientSocket;
    private int port;
    private String username;
    private ObjectOutputStream out;
    private ObjectInputStream in;

    @Override
    public String toString(){
        return "Client[ip= "+port+" ]";
    }
    public int getPort() {
        return port;
    }

    public ObjectOutputStream getOut() {
        return out;
    }

    public ObjectInputStream getIn() {
        return in;
    }

    public  clientHandler(Socket socket){
        this.clientSocket = socket;
        port = socket.getPort();
    }
    @Override
    public void run(){

        try {
            out = new ObjectOutputStream(clientSocket.getOutputStream());
            in = new ObjectInputStream(clientSocket.getInputStream());
            Account account;
            Messages messages;
            while (true){
                try {
                    System.out.println();
                    Object obj=in.readObject();
                    if(obj instanceof Account){
                        account=(Account)obj;
                        username = account.getUsername();
//                        }
                    }
                    if(obj instanceof Messages){
                        messages = (Messages) obj;
                        if(messages.getMsg().equals("flush")){
                            JSONObject jsonObject = new JSONObject();
                            List<Friend> friends = new ArrayList<>();
                            for(clientHandler clientHandler: clientList.clientlist){
                                friends.add(new Friend(clientHandler.username,clientHandler.port));
                            }
                            String result = JSONObject.toJSONString(friends);
                            jsonObject.put("result",result);
                            out.writeObject(jsonObject);
                        }else {
                            String msg = messages.getMsg();
                            for(clientHandler clientHandler: clientList.clientlist){
                                if(clientHandler.port == messages.getPort()){
                                    clientHandler.out.writeObject(new Messages(msg,username,port));
                                }
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    break;
                }
            }




        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(in != null){
                    in.close();
                }
                if(out != null){
                    out.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
